import 'package:flutter/material.dart';

main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  ValueNotifier<String> valueNotifier = ValueNotifier<String>("111");

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      title: "父子组件通讯",
      home: Scaffold(
        appBar: AppBar(
          title: Text("父子组件通讯"),
        ),
        body: ChildPage(valueNotifier),
        floatingActionButton: FloatingActionButton(
          onPressed: () => valueNotifier.value = "222",
          child: Icon(
            Icons.remove,
            color: Colors.red,
          ),
        ),
      ),
    );
  }
}

class ChildPage extends StatefulWidget {
  ValueNotifier<String> valueNotifier;
  ValueChanged daa;
  ChildPage(this.valueNotifier, {key}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _ChildPageState();
  }
}

class _ChildPageState extends State<ChildPage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Text(info);
  }

  String info = "";
  var listener;

  @override
  void dispose() {
    widget.valueNotifier.removeListener(this.listener);
  }

  @override
  void initState() {
    info = widget.valueNotifier.value;
    this.listener = () => setState(() {
          info = widget.valueNotifier.value;
        });
    widget.valueNotifier.addListener(listener);
  }
}
